package com.ruoyi.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserAddresses;
import com.ruoyi.user.service.IUserAddressesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户收货地址Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/user/addresses")
public class UserAddressesController extends BaseController
{
    @Autowired
    private IUserAddressesService userAddressesService;

    /**
     * 查询用户收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('user:addresses:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAddresses userAddresses)
    {
        startPage();
        List<UserAddresses> list = userAddressesService.selectUserAddressesList(userAddresses);
        return getDataTable(list);
    }

    /**
     * 导出用户收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('user:addresses:export')")
    @Log(title = "用户收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAddresses userAddresses)
    {
        List<UserAddresses> list = userAddressesService.selectUserAddressesList(userAddresses);
        ExcelUtil<UserAddresses> util = new ExcelUtil<UserAddresses>(UserAddresses.class);
        util.exportExcel(response, list, "用户收货地址数据");
    }

    /**
     * 获取用户收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:addresses:query')")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") String addressId)
    {
        return success(userAddressesService.selectUserAddressesByAddressId(addressId));
    }

    /**
     * 新增用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('user:addresses:add')")
    @Log(title = "用户收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAddresses userAddresses)
    {
        return toAjax(userAddressesService.insertUserAddresses(userAddresses));
    }

    /**
     * 修改用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('user:addresses:edit')")
    @Log(title = "用户收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAddresses userAddresses)
    {
        return toAjax(userAddressesService.updateUserAddresses(userAddresses));
    }

    /**
     * 删除用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('user:addresses:remove')")
    @Log(title = "用户收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable String[] addressIds)
    {
        return toAjax(userAddressesService.deleteUserAddressesByAddressIds(addressIds));
    }

    /**
     * 根据用户ID查询用户收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('user:myaddresses:query')")
    @GetMapping("/customer/{customerId}")
    public AjaxResult getAddressesByCustomerId(@PathVariable("customerId") String customerId)
    {
        List<UserAddresses> list = userAddressesService.selectUserAddressesByCustomerId(customerId);
        return success(list);
    }

    /**
     * 根据用户ID和地址ID修改用户收货地址
     */
    @PreAuthorize("@ss.hasPermi('user:myaddress:edit')")
    @Log(title = "用户收货地址", businessType = BusinessType.UPDATE)
    @PutMapping("/customer/{customerId}/address/{addressId}")
    public AjaxResult editByCustomerIdAndAddressId(@PathVariable("customerId") String customerId,
                                                   @PathVariable("addressId") String addressId,
                                                   @RequestBody UserAddresses userAddresses)
    {
        userAddresses.setCustomerId(customerId);
        userAddresses.setAddressId(addressId);
        return toAjax(userAddressesService.updateUserAddressByCustomerIdAndAddressId(userAddresses));
    }

    /**
     * 根据用户ID和地址ID查询用户收货地址详情
     */
    @PreAuthorize("@ss.hasPermi('user:myaddress:query')")
    @GetMapping("/customer/{customerId}/address/{addressId}")
    public AjaxResult getAddressByCustomerIdAndAddressId(@PathVariable("customerId") String customerId,
                                                        @PathVariable("addressId") String addressId)
    {
        UserAddresses userAddress = userAddressesService.selectUserAddressByCustomerIdAndAddressId(customerId, addressId);
        return success(userAddress);
    }

    /**
     * 根据用户ID和地址ID删除用户收货地址（软删除）
     */
    @PreAuthorize("@ss.hasPermi('user:myaddress:remove')")
    @Log(title = "用户收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/customer/{customerId}/address/{addressId}")
    public AjaxResult removeByCustomerIdAndAddressId(@PathVariable("customerId") String customerId,
                                                    @PathVariable("addressId") String addressId)
    {
        return toAjax(userAddressesService.deleteUserAddressByCustomerIdAndAddressId(customerId, addressId));
    }
}
